<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use app\erp\validate\Adventrule;
use think\Cache;

class Adventdb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $pk = 'advent_id';

    protected $name = 'advent';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    protected $insert = [
        'partner_id' => PARTNER_ID,
        'advent_name'
    ];

    protected function setAdventNameAttr($value)
    {
        return trim($value);
    }

    public $param = array();

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    public function scopeAdventName($query, $advent_name)
    {
        $query->where('advent_name', 'like', '%' . $advent_name . '%');
    }

    public function scopeAdsenseId($query, $adsense_id)
    {
        $query->where('adsense_id', $adsense_id);
    }
    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        $query->where('partner_id', PARTNER_ID);
        if (isset($this->param['advent_name']) && $this->param['advent_name'] != '') {
            $query->adventName($this->param['advent_name']);
        }
        if (isset($this->param['adsense_id']) && $this->param['adsense_id'] != '') {
            $query->adsenseId($this->param['adsense_id']);
        }
    }

    public function ajaxFind($advent_id)
    {
        $this->param['advent_id'] = $advent_id;
        $data = $this->useGlobalScope(false)
            ->master()
            ->where('advent_id', $this->param['advent_id'])
            ->find();
        if ($data) {
            $data = $data->toArray();
            $data = $this->handleData($data);
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxSelect()
    {
        $return['data'] = $this->ajaxList();
        $return['count'] = $this->ajaxCount();
        return $return;
    }

    public function ajaxList()
    {
        $data = $this->order('adsense_id asc,sort asc,advent_id asc')
            ->page($this->param['page'], $this->param['limit'])
            ->cache($this->cache_name('advent_list'), 86400, 'advent')
            ->select();
        if (! $data->isEmpty()) {
            $data = $data->toArray();
            foreach ($data as $key => $val) {
                $data[$key] = $this->handleData($val);
            }
        }
        return $data;
    }

    public function ajaxCount()
    {
        $count = $this->cache($this->cache_name('advent_count'), 86400, 'advent')
            ->count();
        return $count;
    }

    public function ajaxSave()
    {
        $rule = new Adventrule();
        $this->param['partner_id'] = PARTNER_ID;
        if (isset($this->param['advent_id']) && $this->param['advent_id'] != '' && $this->param['advent_id'] != 0) {
            if (! $rule->scene('edit')->check($this->param)) {
                return $rule->getError();
            }
            unset($this->param['partner_id']);
            $this->save($this->param, [
                'advent_id' => $this->param['advent_id']
            ]);
        } else {
            if (! $rule->check($this->param)) {
                return $rule->getError();
            }
            unset($this->param['partner_id']);
            $this->allowField(true)->save($this->param);
        }
        Cache::clear('advent');
        return 'success';
    }
    // 软删除
    public function ajaxDelete()
    {
        if (isset($this->param['advent_id'])) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('advent_id', 'IN', $this->param['advent_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('advent_id', 'IN', $this->param['advent_id']);
                }, true);
            }
            Cache::clear('advent');
            return 'success';
        }
    }
    // 数据处理
    protected function handleData($val)
    {
        $adsensedb = new Adsensedb();
        foreach ($val as $k => $v) {
            $val[$k] = $v;
            switch ($k) {
                case 'adsense_id':
                    $adsense_name = $adsensedb->ajaxFind($val['adsense_id']);
                    $val['adsense_name'] = $adsense_name['adsense_name'];
                    break;
                case 'advent_picture':
                    $uploaddb = new Uploaddb();
                    $val['advent_picture_info'] = $uploaddb->getFind($val['advent_picture']);
                    break;
                default:
                    break;
            }
        }
        return $val;
    }
}